package wubj.com.tendencydemo.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Map;

/**
 * wubj 创建于 2017/6/7.
 */
public abstract class BaseFragment extends Fragment {

    protected View mRoot;
    protected Context mContext;

    /**
     * params to fragment
     */
    protected Bundle fragmentArgs;
    protected LayoutInflater mInflater;

    /**
     * 此方法可以得到上下文对象
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        if (EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().unregister(this);
//        }
    }

    /**
     * 返回一个需要展示的View
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRoot != null) {
            ViewGroup parent = (ViewGroup) mRoot.getParent();
            if (parent != null)
                parent.removeView(mRoot);
        } else {
            mContext = getActivity();
            mInflater = LayoutInflater.from(mContext);
            fragmentArgs = getArguments();
            getIntentData(fragmentArgs);
            mRoot = initView(inflater);
            initFindViewById(mRoot);
            initData();
            initListener();
        }
        return mRoot;
    }

    protected void getIntentData(Bundle fragmentArgs) {

    }

    /**
     * 子类可以复写此方法初始化事件
     */
    protected void initListener() {

    }

    /**
     * 子类实现此抽象方法返回View进行展示
     *
     * @return
     */
    public abstract View initView(LayoutInflater inflater);

    /**
     * 初始化控件
     */
    protected abstract void initFindViewById(View view);

    /**
     * 子类在此方法中实现数据的初始化
     */
    public abstract void initData();

    public void onEventMainThread(Map<String, Object> inParam) {

    }

//    protected String initHttp(String url, FormBody formBody) {
//        return ((MyBaseActivity) getActivity()).initHttp(url, formBody);
//    }

    protected void hideWaitDialog() {
        FragmentActivity activity = getActivity();
        if (activity instanceof DialogControl) {
            ((DialogControl) activity).hideWaitDialog();
        }
    }

    protected ProgressDialog showWaitDialog() {
        return showWaitDialog("正在加载中...");
    }

    protected ProgressDialog showWaitDialog(int resid) {
        FragmentActivity activity = getActivity();
        if (activity instanceof DialogControl) {
            ((DialogControl) activity).showWaitDialog(resid);
        }
        return null;
    }

    protected ProgressDialog showWaitDialog(String str) {
        FragmentActivity activity = getActivity();
        if (activity instanceof DialogControl) {
            ((DialogControl) activity).showWaitDialog(str);
        }
        return null;
    }

    protected void exitActivity() {
        getActivity().finish();
    }
}
